﻿using Bussiness.Company;
using Bussiness.Language;
using Bussiness.News;
using Bussiness.Product;
using FOModels.Company;
using FOModels.Language;
using FOModels.News;
using FOModels.Product;
using FOSystem.CultureHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace FO.Controllers
{
    public class NewsController : CultureController
    {
        //
        // GET: /News/

        public ActionResult Index(int page)
        {
            int pagetake = 6;
            NewsBLL bll = new NewsBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<NewsModel> list = bll.getNews(lang.LangID);
            ProductBLL probll = new ProductBLL();
            double countlist = list.Count();
            double a = countlist / pagetake;
            double pagesize = Math.Ceiling(a);
            ViewBag.PageSize = pagesize;
            ViewBag.CurrentPage = page;
            return View(list.Skip((page - 1) * pagetake).Take(pagetake));
        }
        public ActionResult getNewsByID()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            NewsBLL newsbll = new NewsBLL();
            NewsModel blognews = newsbll.getTop1NewsByID(lang.LangID);
            if (blognews != null)
            {
                ViewBag.ListBlogNews = newsbll.getTop3NewsByNotID(lang.LangID, blognews.NewsID);
            }
            else
            {
                ViewBag.ListBlogNews = null;
            }
            return PartialView(blognews);
        }
        public ActionResult gettop3catebynews()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductBLL probll = new ProductBLL();
            List<ProductCategoryModels> list = probll.gettop3catebynews(lang.LangID);
            return PartialView(list);
        }
        public ActionResult NewsCate(int page,int ProductCID)
        {
            int pagetake = 6;
            NewsBLL bll = new NewsBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<NewsModel> list = bll.getNewsByProCate(ProductCID,lang.LangID);
            ProductBLL probll = new ProductBLL();
            double countlist = list.Count();
            double a = countlist / pagetake;
            double pagesize = Math.Ceiling(a);
            ViewBag.PageSize = pagesize;
            ViewBag.CurrentPage = page;
            return View(list.Skip((page - 1) * pagetake).Take(pagetake));
        }
        [Authorize(Roles = "8,9")]
        public ActionResult ManagerNews()
        {
            return View();
        }
        public ActionResult getReferenceNews(int ProductID,int NewsID)
        {
            NewsBLL bll = new NewsBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<NewsModel> list = bll.getReferenceNews(2, ProductID,NewsID, lang.LangID);
            return PartialView(list);
        }

        public ActionResult HotestNews()
        {
            NewsBLL bll = new NewsBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            NewsModel list = bll.getTOPNewsByNewsHot(1, lang.LangID);
            return PartialView(list);
        }
        public ActionResult getTOPNewsByNewsHotAndProCate(int ProductCID)
        {
            NewsBLL bll = new NewsBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            NewsModel list = bll.getTOPNewsByNewsHotAndProCate(1,ProductCID, lang.LangID);
            return PartialView(list);
        }
        public ActionResult ProductList()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductBLL probll = new ProductBLL();
            List<ProductCategoryModels> list = probll.getProductCate(lang.LangID);
            return PartialView(list);
        }
        public ActionResult NewsDetail(int NewsID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            NewsBLL cardbll = new NewsBLL();
            NewsModel list = new NewsModel();
            list = cardbll.getNewsByIDToGet(NewsID, lang.LangID);
            return View(list);
        }
        public JsonResult getNewsJson()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            NewsBLL cardbll = new NewsBLL();
            List<NewsModel> list = new List<NewsModel>();
            list = cardbll.getall(lang.LangID);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertNews()
        {
            CompanyBLL combll = new CompanyBLL();
            NewsBLL cardbll = new NewsBLL();
            ProductBLL probll = new ProductBLL();
            LanguageBLL langbll = new LanguageBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            List<ProductModels> pro = new List<ProductModels>();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            com = combll.getCompany(lang.LangID);
            pro = probll.getProductBy(lang.LangID);
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name");
            ViewBag.ProductListID = new SelectList(pro, "ProductID", "ProductName");

            return View();
        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertNews(FO.Models.NewsInsert card)
        {
            CompanyBLL combll = new CompanyBLL();
            NewsBLL cardbll = new NewsBLL();
            ProductBLL probll = new ProductBLL();
            LanguageBLL langbll = new LanguageBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            List<ProductModels> pro = new List<ProductModels>();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            com = combll.getCompany(lang.LangID);
            pro = probll.getProductBy(lang.LangID);
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name");
            ViewBag.ProductListID = new SelectList(pro, "ProductID", "ProductName");

            if (ModelState.IsValid)
            {
                NewsModel items = new NewsModel();
                NewsModel itemsLang = new NewsModel();
                items.ProductID = card.ProductID;
                items.CompanyID = card.CompanyID;
                items.CreateUser = User.Identity.Name;
                items.Enable = false;
                if (card.NewsImg == "")
                {
                    items.NewsImg = "no-image-news.gif";
                }
                else
                {
                    items.NewsImg = card.NewsImg;
                }
                items.NewsDeadline = card.NewsDeadline;
                items.NewsContent = WebUtility.HtmlEncode(card.NewsContent);
                items.NewsSumary = WebUtility.HtmlEncode(card.NewsSumary);
                items.NewsTitle = card.NewsTitle;
                items.CreateUser = User.Identity.Name;
                items.NewsHot = items.NewsHot;
                ////
                itemsLang.ProductID = card.ProductID;
                itemsLang.CompanyID = card.CompanyID;
                itemsLang.CreateUser = User.Identity.Name;
                itemsLang.Enable = false;
                if (card.NewsImg != "")
                {
                    itemsLang.NewsImg = "no-image-news.gif";
                }
                else
                {
                    itemsLang.NewsImg = card.NewsImg;
                }
                itemsLang.NewsDeadline = card.NewsDeadline;
                itemsLang.NewsContent = WebUtility.HtmlEncode(card.NewsContentLang);
                itemsLang.NewsSumary = WebUtility.HtmlEncode(card.NewsSumaryLang);
                itemsLang.NewsTitle = card.NewsTitleLang;
                itemsLang.NewsHot = items.NewsHot;
                itemsLang.CreateUser = User.Identity.Name;
                cardbll.InsertNews(items);
                NewsModel carditem = cardbll.getTOP1();
                items.NewsID = carditem.NewsID;
                itemsLang.NewsID = carditem.NewsID;
                cardbll.InsertNewsLanng(items, 1);
                cardbll.InsertNewsLanng(itemsLang, 2);
                return RedirectToAction("ManagerNews");
            }
            return View(card);
        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateNews(int NewsID)
        {
            CompanyBLL combll = new CompanyBLL();
            NewsBLL cardbll = new NewsBLL();
            ProductBLL probll = new ProductBLL();
            LanguageBLL langbll = new LanguageBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            List<ProductModels> pro = new List<ProductModels>();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            com = combll.getCompany(lang.LangID);
            pro = probll.getProductBy(lang.LangID);
            Models.News c = new Models.News();
            NewsModel card = cardbll.getNewsByID(NewsID, lang.LangID);
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name", card.CompanyID);
            ViewBag.ProductListID = new SelectList(pro, "ProductID", "ProductName", card.ProductID);
            c.NewsID = card.NewsID;
            c.ProductID = card.ProductID;
            c.CompanyID = card.CompanyID;
            c.ViewCount = card.ViewCount;
            c.CreateDate = card.CreateDate;
            c.CreateUser = card.CreateUser;
            c.UpdateDate = card.UpdateDate;
            c.UpdateUser = card.UpdateUser;
            c.Enable = card.Enable;
            if (card.NewsImg == "")
            {
                c.NewsImg = "no-image-news.gif";
            }
            else
            {
                c.NewsImg = card.NewsImg;
            }
            c.NewsDeadline = card.NewsDeadline;
            c.NewsTitle = card.NewsTitle;
            c.NewsHot = card.NewsHot;
            c.NewsSumary = WebUtility.HtmlDecode(card.NewsSumary);
            c.NewsContent = WebUtility.HtmlDecode(card.NewsContent);
            return View(c);
        }
        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateNews(FO.Models.News card)
        {

            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            NewsBLL cardbll = new NewsBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            List<ProductModels> pro = new List<ProductModels>();
            NewsModel ca = new NewsModel();
            ca = cardbll.getNewsByID(card.NewsID, lang.LangID);
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name", ca.CompanyID);
            ViewBag.ProductListID = new SelectList(pro, "ProductID", "ProductName", ca.ProductID);
            if (ca != null)
            {
                ca.NewsID = card.NewsID;
                ca.ProductID = card.ProductID;
                ca.CompanyID = card.CompanyID;
                ca.ViewCount = card.ViewCount;
                ca.CreateDate = card.CreateDate;
                ca.CreateUser = card.CreateUser;
                ca.UpdateUser = User.Identity.Name;
                ca.Enable = card.Enable;
                if (card.NewsImg == "")
                {
                    ca.NewsImg = "no-image-news.gif";
                }
                else
                {
                    ca.NewsImg = card.NewsImg;
                }
                ca.NewsDeadline = card.NewsDeadline;
                ca.NewsTitle = card.NewsTitle;
                ca.NewsHot = card.NewsHot;
                ca.NewsSumary = WebUtility.HtmlEncode(card.NewsSumary);
                ca.NewsContent = WebUtility.HtmlEncode(card.NewsContent);
                cardbll.UpdateNews(ca, lang.LangID);

                return RedirectToAction("ManagerNews");
            }
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeleteNews(int NewsID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            NewsBLL bll = new NewsBLL();
            NewsModel card = new NewsModel();
            card = bll.getNewsByID(NewsID, lang.LangID);
            if (card != null)
            {
                bll.DeleteNews(card.NewsID);
                var result = new { Status = "OK" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var result = new { Status = "Xóa thất bại" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }


        }


    }
}
